package com.renfei.camerapreviewdemo.presenter;

import android.app.Activity;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.opengl.EGLContext;
import android.util.Log;

import androidx.annotation.NonNull;

import com.cgfay.uitls.utils.BrightnessUtils;
import com.renfei.camerapreviewdemo.camera.CameraController;
import com.renfei.camerapreviewdemo.camera.CameraParam;
import com.renfei.camerapreviewdemo.camera.ICameraController;
import com.renfei.camerapreviewdemo.camera.listener.OnFrameAvailableListener;
import com.renfei.camerapreviewdemo.camera.listener.OnSurfaceTextureListener;
import com.renfei.camerapreviewdemo.camera.listener.PreviewCallback;
import com.renfei.camerapreviewdemo.fragment.CameraPreviewFragment;
import com.renfei.camerapreviewdemo.render.CameraRenderer;

public class CameraPreviewPresenter extends PreviewPresenter<CameraPreviewFragment> implements
    PreviewCallback,OnFrameAvailableListener, OnSurfaceTextureListener
{


    private static final String TAG = CameraPreviewPresenter.class.getSimpleName();
    // 预览参数
    private CameraParam mCameraParam;
    private Activity mActivity;
    // 相机接口
    private ICameraController mCameraController;

    // 渲染器
    private final CameraRenderer mCameraRenderer;


    public CameraPreviewPresenter(CameraPreviewFragment target) {
        super(target);
        mCameraParam = CameraParam.getInstance();
        mCameraRenderer = new CameraRenderer(this);
    }


    public void onAttach(Activity activity) {
        mActivity = activity;

        mCameraRenderer.initRenderer();

        mCameraController = new CameraController(mActivity);
        mCameraController.setPreviewCallback(this);
        mCameraController.setOnFrameAvailableListener(this);
        mCameraController.setOnSurfaceTextureListener(this);


        if (BrightnessUtils.getSystemBrightnessMode(mActivity) == 1) {
            mCameraParam.brightness = -1;
        } else {
            mCameraParam.brightness = BrightnessUtils.getSystemBrightness(mActivity);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
    }
    @Override
    public void onResume() {
        super.onResume();
        openCamera();
    }
    @Override
    public void onPause() {
        super.onPause();
        mCameraRenderer.onPause();
        closeCamera();
    }
    @Override
    public void onStop() {
        super.onStop();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    public void onDetach() {
        mActivity = null;
        mCameraRenderer.destroyRenderer();
    }

    @NonNull
    @Override
    public Context getContext() {
        return mActivity;
    }



    //***********相机监听************************************************************************************

    /**
     * 相机输出的帧可用
     * @param surfaceTexture
     */
    @Override
    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
        Log.e(TAG, "onFrameAvailable: ");

    }

    @Override
    public void onSurfaceTexturePrepared(@NonNull SurfaceTexture surfaceTexture) {
        mCameraRenderer.bindInputSurfaceTexture(surfaceTexture);
    }

    /**
     * 相机预览数据回调
     * @param data
     */
    @Override
    public void onPreviewFrame(byte[] data) {
        Log.d(TAG, "onPreviewFrame: ");
        mCameraRenderer.requestRender();
    }

   //***********SurfaceTexture监听************************************************************************************
    @Override
    public void onSurfaceCreated(SurfaceTexture surfaceTexture) {
        mCameraRenderer.onSurfaceCreated(surfaceTexture);
    }

    @Override
    public void onSurfaceChanged(int width, int height) {
        mCameraRenderer.onSurfaceChanged(width, height);
    }

    @Override
    public void onSurfaceDestroyed() {
        mCameraRenderer.onSurfaceDestroyed();
    }


    @Override
    public void onBindSharedContext(EGLContext context) {
        Log.d(TAG, "onBindSharedContext: ");
    }

    @Override
    public void takePicture() {
    }

    @Override
    public void switchCamera() {
        mCameraController.switchCamera();
    }

    /**
     * 打开相机
     */
    private void openCamera() {
        mCameraController.openCamera();
        calculateImageSize();
    }
    /**
     * 计算imageView 的宽高
     */
    private void calculateImageSize() {
        int width;
        int height;
        if (mCameraController.getOrientation() == 90 || mCameraController.getOrientation() == 270) {
            width = mCameraController.getPreviewHeight();
            height = mCameraController.getPreviewWidth();
        } else {
            width = mCameraController.getPreviewWidth();
            height = mCameraController.getPreviewHeight();
        }
        mCameraRenderer.setTextureSize(width, height);
    }
    /**
     * 关闭相机
     */
    private void closeCamera() {
        mCameraController.closeCamera();
    }

}
